<extend name="Public:base" />
<block name="content">
    <div class="row">
        <div class="col-xs-12">
            <form class="form-horizontal"  id="validation-form" method="post" onsubmit="return check()" action='{:U("Coupon/addcoupon")}'>
                <div class="page-header">
                    <button id="add" class="btn btn-primary btn-sm radius5">添加</button>
                    <a href="{:U('Coupon/manage')}" class="btn btn-primary btn-sm radius5">返回</a>
                </div>
                <div class="form-group required">
                    <label class="col-sm-2 control-label">电子券名称：</label>
                    <div class="col-sm-10">
                        <input  class="form-control" type="text" placeholder="" name="volume_name"  value=""  style="width:200px;"/>
                    </div>
                </div>
                <div class="form-group required">
                    <label class="col-sm-2 control-label">面值：</label>
                    <div class="col-sm-10">
                        <input  class="form-control" type="text" placeholder="" name="total"  value="" style="width:200px;display:inline;"/> 元
                    </div>
                </div>
                <div class="form-group required">
                    <label class="col-sm-2 control-label">订单需满：</label>
                    <div class="col-sm-10">
                        <input  class="form-control" type="text" placeholder="" name="min"  value="" style="width:200px;display:inline;"/> 元
                    </div>
                </div>
                <div class="form-group required">
                    <label class="col-sm-2 control-label">类型：</label>
                    <label><input type="radio"  name="type" value="1" onclick="" checked="checked"/>代金券</label>&nbsp;&nbsp;&nbsp;&nbsp;
                    <label><input type="radio" name="type" value="2" onclick="" />现金券</label>
                </div>
                <div class="form-group required">
                    <label class="col-sm-2 control-label">可用范围：</label>
                    <div class="col-sm-10">
                        <ul id="treeDemo" class="ztree">
                        </ul>
                        <input type="hidden" id="tree" name="category" value=""/>
                    </div>
                </div>
                <div class="form-group required">
                    <label class="col-sm-2 control-label">状态：</label>
                    <label><input type="radio"  name="status" value="1" onclick="" checked="checked"/>开启</label>&nbsp;&nbsp;&nbsp;&nbsp;
                    <label><input type="radio" name="status" value="0" onclick="" />关闭</label>
                </div>
                <div class="form-group required">
                    <label class="col-sm-2 control-label">有效期：</label>
                    <label>开始时间：<input id="startDate" name="start_time" class="Wdate" type="text" onfocus="WdatePicker({dateFmt:'yyyy-M-d 00:00:00',minDate:'%y-%M-{%d'})"/></label>&nbsp;&nbsp;&nbsp;&nbsp;
                    <label>结束时间：<input id="endDate" name="end_time" class="Wdate" type="text" onfocus="WdatePicker({dateFmt:'yyyy-M-d 00:00:00',minDate:'%y-%M-{%d}'})"/></label>
                </div>
            </form>
        </div>
    </div>
</block>
<block name="javascript">
    <script type="text/javascript" src="Common/js/My97DatePicker/WdatePicker.js"></script>
    <link href="Common/js/zTree_v3-master/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" type="text/css" />
    <script src="Common/js/zTree_v3-master/js/jquery.ztree.core.js" type="text/javascript"></script>
    <script src="Common/js/zTree_v3-master/js/jquery.ztree.excheck.js" type="text/javascript"></script>
    <script type="text/javascript">
        function onCheck(e, treeId, treeNode) {
            var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
            var nodes = treeObj.getCheckedNodes(true);

            var idList = "";
            $.each(nodes, function (index, info) {
                if (idList != "") {
                    idList += ",";
                }

                idList += info.id;
            });
            $("#tree").val(idList);
        }
        var setting = {
            check: {
                enable: true
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "pId",
                    rootPId: 0
                }
            },
            check: {
                enable: true,
                chkStyle: "checkbox",
                chkboxType: { "Y": "", "N": "" }
            },
            callback: {
                onCheck: onCheck
            }

        };
        setting.check.chkboxType = { "Y" : "ps", "N" : "ps" };
        setting.data.simpleData ={enable: true, idKey: "id", pIdKey: "pId", rootPId: 0};
        setting.data.key;
        var zNodes;

        $.ajax({
            url:"{:U('Coupon/range')}",
            type:"get",
            dataType:'json',
            success:function(data){
                zNodes=data;
                $.fn.zTree.init($("#treeDemo"), setting, zNodes);
            }
        });

        function check(){

            var volume_name = $('input[name=volume_name]').val();
            var total = $('input[name=total]').val();
            var min = $('input[name=min]').val();
            var start_time = $('input[name=start_time]').val();
            var end_time = $('input[name=end_time]').val();
            var category = $('input[name=category]').val();
            if($.trim(volume_name)==''){
                $.jbox.info('名称不能为空！','提示');
                return false;
            }
            if($.trim(total)==''){
                $.jbox.info('价格不能为空！','提示');
                return false;
            }
            if(isNaN($.trim(total))){
                $.jbox.info('请输入数字！','提示');
                return false;
            }
            if($.trim(total)==0){
                $.jbox.info('价格不能为零！','提示');
                return false;
            }
            if($.trim(min)==''){
                $.jbox.info('条件不能为空！','提示');
                return false;
            }
            if(isNaN($.trim(min))){
                $.jbox.info('请输入数字！','提示');
                return false;
            }
            if(category==''){
                $.jbox.info('范围不能为空！','提示');
                return false;
            }
            if($.trim(start_time)==''|| $.trim(end_time)==''){
                $.jbox.info('请设置有效期!','提示');
                return false;
            }
            $('#validation-form').ajaxSubmit(function(data){
                if(data=='1'){
                    $.jbox.tip("添加成功！", 'loading');
                    window.setTimeout(function () {window.location.href="{:U('Coupon/manage')}"; }, 500);
                }if(data=='2') {
                    $.jbox.info('没有任何修改！', '提示');
                }
            });

            return false;
        }
    </script>
</block>